package com.service;

import com.mapper.FinishedProductMapper;
import com.mapper.ProductOrderMapper;
import com.mapper.sales.SalesOrderMapper;
import com.pojo.FinishedProductInfo;
import com.pojo.Hua.HuaProductionOrder;
import com.pojo.sales.ProductOrder;
import com.service.iter.IProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 产品订单
 *
 * @author admin
 * 2023/06/20
 * @date 2023/06/20
 */
@Service
@Transactional

public class ProductOrderImpl implements IProductOrderService {


    /**
     * 产品订单映射器
     */
    @Autowired
    ProductOrderMapper productOrderMapper;
    @Autowired
    FinishedProductMapper finishedProductMapper;
    @Autowired
    SalesOrderMapper salesOrderMapper;

    /**
     * 查询所有
     *
     * @return {@link List}<{@link ProductOrder}>
     */
    @Override
    public List<ProductOrder> QueryAll() {
        return productOrderMapper.QueryAll();
    }

public HashMap<String, String> addOrder( ProductOrder productOrder,Long id){
    HashMap<String, String> map = new HashMap<>();
    Long productId = productOrder.getProductId();
    Long productNum = productOrder.getProductNumber();
    Long sizeId =productOrder.getSizeId();
    Long categoryId =productOrder.getCategoryId();
    final String productOrderCode = productOrder.getProductOrderCode();
    Long productMaterial =productOrder.getProductMaterial();
    Long productOrderStatus = productOrder.getProductOrderStatus();

    //成品数量为0或没有该产品
    //修改订单状态、添加一条生产订单数据、修改销售订单状态
    productOrderMapper.updateStatus(id, 4);
    productOrderMapper.add(productId,productNum,sizeId,categoryId,productOrderCode,productMaterial,productOrderStatus);
    map.put("code","200");
    map.put("msg","订单生成成功");
    return map;
}

    /**
     * 检查
     *
     * @param id     ID
     * @param sizeId 尺寸标识
     * @return {@link HashMap}<{@link String}, {@link String}>
     */
    public HashMap<String, String> CheckOut(Long id,Long sizeId) {
        ProductOrder productOrder = productOrderMapper.queryProductOrderById(id);
        HashMap<String, String> map = new HashMap<>();
        map.put("code","400");
        map.put("msg","订单生成失败");
        //产品订单的需求量
        Long productId = productOrder.getProductId();
        List<FinishedProductInfo> finishedProductInfos = finishedProductMapper.QueryFinishProductById(productId,sizeId);
        System.out.println(finishedProductInfos.toString());
        if (Objects.equals(finishedProductInfos.toString(),"[]")) {
            Long productNum = productOrder.getProductNumber();
            productOrderMapper.updateStatus(id, 4);
            salesOrderMapper.updateStatesByNumber(7L,productOrder.getProductOrderCode());
            productOrderMapper.add(productId, productNum, sizeId,
                    productOrder.getCategoryId(), productOrder.getProductOrderCode(), productOrder.getProductMaterial(),
                    productOrder.getProductOrderStatus());

            map.put("code","200");
            map.put("msg","创建生产订单");
        }else {
            for (FinishedProductInfo finishedProductInfo : finishedProductInfos) {
            Long productNum = productOrder.getProductNumber();
            Long finishProductNum = finishedProductInfo.getFinishedProductNum();
            if (finishProductNum <1) {
                return  addOrder(productOrder, id);
            }else if (productNum <= finishProductNum) {
                //成品数量大于需求量
                //修改订单状态、直接发货、修改销售订单状态
                productOrderMapper.updateStatus(id, 3);
                map.put("code","201");
                map.put("msg","订单可直接发货");
                return map;
            }else {
                //成品数量小于需求量同时成品数量大于0
                //修改订单状态、将需求量减去成品数量的值作为添加生产订单中的数量参数、修改销售订单状态为7
                productOrderMapper.updateStatus(id, 4);
                Long productionOrderNum = productNum - finishProductNum;
                Long finishedProductId = finishedProductInfo.getFinishedProductId();
                System.out.println(productionOrderNum);
                if (productionOrderNum>0L) {
                    finishedProductMapper.updateNumber(finishedProductId,0L);
                }else {
                    //生产订单需要生产的产品数量
                    long productNumber = Math.abs(productionOrderNum);
                    finishedProductMapper.updateNumber(finishedProductId,productNumber);
                }
                productOrderMapper.add(productId, productionOrderNum, sizeId,
                        productOrder.getCategoryId(), productOrder.getProductOrderCode(), productOrder.getProductMaterial(),
                        productOrder.getProductOrderStatus());
                map.put("code", "202");
                map.put("msg", "成品出货成功");
                return map;
            }
        }}

        return map;
    }
    @Override
    public List<HuaProductionOrder> queryAll() {
        return null;
    }
    @Override

    public HashMap<Object, Object> UpdateStatus(Long id) {
        HashMap<Object, Object> map = new HashMap<>();
        String  salesOrderNo1 = productOrderMapper.queryProductId(id).getSalesOrderNo();
        if (Objects.isNull(salesOrderNo1)) {
            map.put("code", "400");
            map.put("msg", "此订单数据异常");
            return map;
        }
            if (salesOrderMapper.updateStatesByNumber(11L,salesOrderNo1)<0) {
                try {
                    throw new  Exception("订单修改状态失败");
                }catch (Exception e){
                    map.put("code", "400");
                    map.put("msg", "订单修改态失败");
                    return map;
                }
            }
            if (productOrderMapper.UpdateState(id,2L,11)<0) {
                    try {
                        throw new  Exception("订单修改状态失败");
                    }catch (Exception e){
                        map.put("code", "400");
                        map.put("msg", "订单修改态失败");
                        return map;
                    }
                } else {
                    map.put("code", "200");
                    map.put("msg", "拒绝成功");
                    return map;
                }
        }
}